//
//  TNMarkHistoryViewController.m
//  跑跑快销
//
//  Created by ylife on 14-4-30.
//  Copyright (c) 2014年 ylife. All rights reserved.
//

#import "TNMarkHistoryViewController.h"
#import "TNMarkHistoryTableViewCell.h"
#import "YXCalendarView.h"

@interface TNMarkHistoryViewController ()

@end

@implementation TNMarkHistoryViewController
{
    BOOL isSelectRightButton;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle:@"团员的签到记录"];
        isSelectRightButton = NO;
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //添加导航栏右键
    _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50 , 30)];
    [_rightButton setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
    _rightButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_rightButton addTarget:self action:@selector(gotoMapViewController) forControlEvents:UIControlEventTouchUpInside];
    
    //更改返回按钮标题
    UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] init];
    backBarButtonItem.title = @"返回";
    self.navigationItem.backBarButtonItem = backBarButtonItem;
    
    UIBarButtonItem *mapButton = [[UIBarButtonItem alloc]initWithCustomView:_rightButton];
    [self.navigationItem setRightBarButtonItem:mapButton];
    
    _markHistoryView = [[TNMenberMarkHistoryView alloc]init];
    self.view = _markHistoryView;
    _markHistoryView.markHistoryTableView.dataSource = self;
    _markHistoryView.markHistoryTableView.delegate = self;
    
    //为日历按钮添加点击事件
    [_markHistoryView.dateChooseButton addTarget:self action:@selector(showCalendar:) forControlEvents:UIControlEventTouchUpInside];
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *nowdate = [NSDate date];
    NSString *datastring = [formatter stringFromDate:nowdate];
    _markHistoryView.calendarLable.text = datastring;
    
    
    
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [_dateBackButton removeFromSuperview];
    [_window removeFromSuperview];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 按钮点击事件
- (void)showCalendar:(UIButton *)btn
{
    if (!_dateBackButton) {
        _window = [[UIApplication sharedApplication].windows objectAtIndex:0];
        _dateBackButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _dateBackButton.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3];
        _dateBackButton.frame = [UIScreen mainScreen].bounds;
        [_dateBackButton addTarget:self action:@selector(touchDayBackBtn) forControlEvents:UIControlEventTouchUpInside];
        [_window addSubview:_dateBackButton];
        
        YXCalendarView * dayCalendar = [[YXCalendarView alloc] initWithFrame:CGRectMake(0, btn.frame.origin.y + btn.frame.size.height + STARTY, 320, 325) withBlock:^(NSString * str) {
            [_markHistoryView.calendarLable setText:str];
            _dateBackButton.hidden = YES;
        }];
        [_dateBackButton addSubview:dayCalendar];
    }
    [_dateBackButton setHidden:NO];
   

}

- (void)touchDayBackBtn
{
    [_dateBackButton setHidden:YES];
}
- (void)gotoMapViewController
{
    if (!isSelectRightButton) {
        NSLog(@"去地图界面");
        isSelectRightButton = !isSelectRightButton;
        [_rightButton setImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateNormal];
        [_markHistoryView hideTableView];
        [_markHistoryView showMapView];
        
    }
    else{
        NSLog(@"去签到历史界面");
        [_rightButton setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
        isSelectRightButton = !isSelectRightButton;
        [_markHistoryView showTableView];
        [_markHistoryView hideMapView];
    }
}

#pragma mark - tableview代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"markHistoryCell";
    TNMarkHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[TNMarkHistoryTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.signImageView.image = [UIImage imageNamed:@"a.png"];
    cell.nameLable.text = @"苏果便利阳澄湖店";
    cell.signLable.text = @"网点签到";
    
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH:mm"];
    NSString *dateString = [formatter stringFromDate:date];
    cell.dateLable.text = dateString;
    if (indexPath.row == 0) {
        [cell hideHighLineView];
    }
    else if (indexPath.row == 4)
    {
        [cell hideLowLineView];
    }
    return cell;
}




@end
